package com.xuef.ibatis.builder.xml;

import com.xuef.ibatis.builder.BaseBuilder;
import com.xuef.ibatis.builder.BuilderException;
import com.xuef.ibatis.parsing.XNode;
import com.xuef.ibatis.parsing.XPathParser;
import com.xuef.ibatis.session.Configuration;

import java.io.InputStream;
import java.util.Properties;

/**
 * Created by xuefengDong: moveb
 * On 2020/6/20- 20:28
 * @description 解析配置文件
 */
public class XMLConfigBuilder extends BaseBuilder {
    boolean parsed;
    XPathParser parser;
    String environment;

    public XMLConfigBuilder(InputStream inputStream) {
        this(
                new XPathParser(inputStream)
        );
    }
    public XMLConfigBuilder(XPathParser xPathParser){
        this.parser = xPathParser;
    }
    public XMLConfigBuilder(){}

    public Configuration parse() {
        if(parsed){
            //TODO 抛出异常，提示已经解析
        }
        parsed = true;
        parseConfiguration(parser.evalRoot("configuration"));

        return configuration;
    }

    private void parseConfiguration(XNode root) {
        try {
            propertiesElement(root.evalNode("properties")); //issue #117 read properties first
            settingsElement(root.evalNode("settings"));
            typeAliasesElement(root.evalNode("typeAliases"));
            pluginElement(root.evalNode("plugins"));
            objectFactoryElement(root.evalNode("objectFactory"));
            objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
            databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            typeHandlerElement(root.evalNode("typeHandlers"));
            mapperElement(root.evalNode("mappers"));
        } catch (Exception e) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
        }
    }



    //TODO taotao
    private void propertiesElement(XNode context) throws Exception {

    }

    //TODO
    private void typeAliasesElement(XNode context) throws Exception {
    }

    //TODO
    private void pluginElement(XNode context) throws Exception {
    }
    //TODO
    private void objectFactoryElement(XNode context) throws Exception {
    }
    //TODO
    private void objectWrapperFactoryElement(XNode context) throws Exception {
    }
    //TODO
    private void settingsElement(XNode context) throws Exception {
    }
    //TODO
    private void environmentsElement(XNode context) throws Exception {
    }
    //TODO
    private void databaseIdProviderElement(XNode context) throws Exception {
    }
    //TODO
    private void typeHandlerElement(XNode context) throws Exception {
    }

    //TODO
    private void mapperElement(XNode context) throws Exception {
    }
}
